﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsCertificatesTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Certificates Business Object
    /// </summary>

    public class Certificates : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Certificates() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// CertificateID.
        /// </summary>
        public int CertificateID
        {
            get { return _certificateID; }
            set
            {
                if (_certificateID != value)
                {
                    if (OnPropertyChanging("CertificateID", _certificateID, value))
                    {
                        int oldValue = _certificateID;
                        _certificateID = value;
                        OnPropertyChanged("CertificateID", oldValue, value);
                    }
                }
            }
        }  private int _certificateID = -1;


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// CertificateName.
        /// </summary>
        public string CertificateName
        {
            get { return _certificateName; }
            set
            {
                if (_certificateName != value)
                {
                    if (OnPropertyChanging("CertificateName", _certificateName, value))
                    {
                        string oldValue = _certificateName;
                        _certificateName = value;
                        OnPropertyChanged("CertificateName", oldValue, value);
                    }
                }
            }
        }  private string _certificateName = null;



        /// <summary>
        /// IssuingAuthority.
        /// </summary>
        public string IssuingAuthority
        {
            get { return _issuingAuthority; }
            set
            {
                if (_issuingAuthority != value)
                {
                    if (OnPropertyChanging("IssuingAuthority", _issuingAuthority, value))
                    {
                        string oldValue = _issuingAuthority;
                        _issuingAuthority = value;
                        OnPropertyChanged("IssuingAuthority", oldValue, value);
                    }
                }
            }
        }  private string _issuingAuthority = null;



        /// <summary>
        /// IssuingYear.
        /// </summary>
        public int IssuingYear
        {
            get { return _issuingYear; }
            set
            {
                if (_issuingYear != value)
                {
                    if (OnPropertyChanging("IssuingYear", _issuingYear, value))
                    {
                        int oldValue = _issuingYear;
                        _issuingYear = value;
                        OnPropertyChanged("IssuingYear", oldValue, value);
                    }
                }
            }
        }  private int _issuingYear = 0;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Certificates object.
        /// </summary>
        public static Certificates CreateNew()
        {
            Certificates obj = new Certificates();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Certificates CreateNew(CertificatesCollection collection)
        {
            Certificates obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetCertificateByID

        /// <summary>
        /// Returns a collection of Certificates.
        /// </summary>
        public static Certificates GetCertificateByID(int certificateid)
        {
            CertificatesTableAdapter adapter = new CertificatesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsCertificates.CertificatesDataTable tbl = adapter.GetData(certificateid);

            if (tbl.Rows.Count == 1)
            {
                Certificates certificates = new Certificates();
                RelationalToObject(certificates, tbl.Rows[0]);
                return (certificates);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'certificateid' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetCertificatesByUserID

        /// <summary>
        /// Returns a collection of certificates for the specified 'UserID' or null if no certificates with the specified parameter(s) exists.
        /// </summary>
        public static CertificatesCollection GetCertificatesByUserID(int userid)
        {
            CertificatesTableAdapter adapter = new CertificatesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsCertificates.CertificatesDataTable tbl = adapter.GetDataByUserID(userid);

            CertificatesCollection list = new CertificatesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsCertificates.CertificatesRow row in tbl.Rows)
                {
                    Certificates certificates = new Certificates();
                    RelationalToObject(certificates, row);
                    list.Add(certificates);
                }
            }

            return (list);
        }

        #endregion

        #region GetCertificateByCertificateName

        /// <summary>
        /// Returns a collection of Certificates.
        /// </summary>
        public static Certificates GetCertificateByCertificateName(string certificateName)
        {
            CertificatesTableAdapter adapter = new CertificatesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsCertificates.CertificatesDataTable tbl = adapter.GetDataByCertificateName(certificateName);

            if (tbl.Rows.Count == 1)
            {
                Certificates certificates = new Certificates();
                RelationalToObject(certificates, tbl.Rows[0]);
                return (certificates);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'certificateName' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Certificates.
        /// </summary>
        public static void Update(Certificates obj)
        {
            bool updateCertificates = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateCertificates = (obj.ObjState != ObjectState.Unchanged);

                if (!updateCertificates)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsCertificates.CertificatesDataTable tbl = null;
            if (updateCertificates)
            {
                CertificatesTableAdapter adapter = new CertificatesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsCertificates.CertificatesDataTable();
                dsCertificates.CertificatesRow row = tbl.NewCertificatesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("JobID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Certificates: UserID={0}, CertificateName={1}, {2}", UserID, CertificateName));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class CertificatesCollection : List<Certificates>
    {
        #region Types

        public class UserComparer : IComparer<Certificates>
        {
            public int Compare(Certificates c1, Certificates c2)
            {
                return (String.Compare(c1.CertificateID.ToString(), c2.CertificateID.ToString()));
            }
        }

        #endregion
    }
}
